package de.hwr.it11.dectree.gui.component;

import java.beans.PropertyChangeEvent;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import de.hwr.it11.dectree.constant.PropertyChangeTypes;
import de.hwr.it11.dectree.controller.AutomaticDecisionTreeBuilder;
import de.hwr.it11.dectree.controller.Controller;
import de.hwr.it11.dectree.language.Messages;
import de.hwr.it11.dectree.model.DecisionTree;
import de.hwr.it11.dectree.model.Project;

/**
 * Stellt einen <code>DecisionTree</code> automatisch dar.
 */
public class AutomaticTreeComposite extends ATreeComposite {

	/** ToolItem zum Aktualisieren des Baumes */
	private ToolItem _refreshToolItem;



	/**
	 * Erstellt ein neues <code>AutomaticTreeComposite</code>.
	 * 
	 * @param pParent
	 *            der Container, in dem diese Instanz erzeugt werden soll
	 * @param pStyle
	 *            die Stylebits dieser Instanz
	 * @param pController
	 *            der zu verwendende <code>Controller</code>
	 * @param pProject
	 *            das zu verwendende <code>Project</code>
	 */
	public AutomaticTreeComposite(Composite pParent, int pStyle, Controller pController, Project pProject) {
		super(pParent, pStyle, pController, pProject);

		ToolBar toolBar = getToolBar();

		_refreshToolItem = new ToolItem(toolBar, SWT.PUSH);
		_refreshToolItem.setToolTipText(Messages.TOOLTIP_REFRESH_TREE);
		_refreshToolItem.setImage(SWTResourceManager.getImage(getClass(), "/img/arrow_refresh.png"));
		_refreshToolItem.setEnabled(false);

		initEventHandler();
	}



	/**
	 * Initialisiert die Event-Methoden der GUI-Komponenten.
	 */
	private void initEventHandler() {
		// Baum aktualisieren
		_refreshToolItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent pE) {
				AutomaticDecisionTreeBuilder builder = new AutomaticDecisionTreeBuilder(getProject().getDynamicTable(),
						getProject().getPreferences());
				builder.build();
				getProject().setAutomaticTree(builder.getDecisionTree());
			}

		});
	}



	@Override
	public void hookUpdate(PropertyChangeEvent pEvt) {
		String propertyName = pEvt.getPropertyName();

		if (PropertyChangeTypes.PROJECT_AUTOMATICTREE.equals(propertyName)) {
			setDecisionTree((DecisionTree) pEvt.getNewValue());
		}
		if (PropertyChangeTypes.PROJECT_DYNAMICTABLE.equals(propertyName)
				|| PropertyChangeTypes.DYNAMICTABLE_GOALATTRIBUTE.equals(propertyName)
				|| PropertyChangeTypes.DYNAMICTABLE_ATTRIBUTE_ADD.equals(propertyName)
				|| PropertyChangeTypes.DYNAMICTABLE_ATTRIBUTE_REMOVE.equals(propertyName)
				|| PropertyChangeTypes.DYNAMICTABLE_RECORD_ADD.equals(propertyName)
				|| PropertyChangeTypes.DYNAMICTABLE_RECORD_REMOVE.equals(propertyName)
				|| PropertyChangeTypes.DYNAMICTABLE_VALUE_SET.equals(propertyName)) {
			_refreshToolItem.setEnabled(getProject().getDynamicTable().isComplete());
		}
		super.hookUpdate(pEvt);
	}



	@Override
	public void languageChanged() {
		_refreshToolItem.setToolTipText(Messages.TOOLTIP_REFRESH_TREE);
		super.languageChanged();
	}

}
